package com.gowhere.controller.basedata;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gowhere.controller.BaseServlet;
import com.gowhere.domain.Orders;
import com.gowhere.service.basedata.OrdersService;
import com.gowhere.service.impl.basedata.OrdersServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/basedata/orders/*")
public class OrdersServlet extends BaseServlet {

    protected void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = 1;
        int pageSize = 3;
        String pageNumStr = req.getParameter("page");
        //System.out.println(pageNumStr);
        String pageSizeStr = req.getParameter("pageSize");
        //System.out.println(pageSizeStr);
        if (StringUtils.isNotBlank(pageNumStr)) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        List<Orders> list = ordersService.findByPage(pageNum, pageSize);
        PageInfo<Orders> pageInfo = new PageInfo<Orders>(list);

        req.setAttribute("page", pageInfo);
        req.getRequestDispatcher("/pages/basedata/orders-list.jsp").forward(req, resp);

    }


    protected void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");

        ordersService.deleteById(ids);

        req.getRequestDispatcher("/basedata/orders/findByPage").forward(req, resp);
    }


    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/pages/basedata/product-add.jsp").forward(req, resp);
    }


    protected void findAllDetails(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("id");
        List<Orders> ordersList = ordersService.findAllDetails(oid);

        for (Orders orders : ordersList) {
            System.out.println(orders);
            req.setAttribute("orders", orders);
        }


        req.getRequestDispatcher("/pages/basedata/orders-show.jsp").forward(req, resp);
    }


    protected void findByLike(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=uft-8");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String action = req.getParameter("action");
        if(action==null){
            System.out.println("无搜索值");
        }else if ("querybylike".equals(action)){
            String keywords = req.getParameter("keywords");
            String likekeywords="%"+keywords+"%";
           List<Orders>list= ordersService.findByLike(likekeywords);
           req.setAttribute("orders",list);
           req.getRequestDispatcher("/pages/basedata/orders-list2.jsp").forward(req,resp);

        }


    }


    protected void turnTolist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        findByPage(req,resp);
    }

    //开启按钮
    protected void updateStatus0ByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");

            ordersService.updateStatus0ByIds(ids);

        req.getRequestDispatcher("/basedata/orders/findByPage").forward(req, resp);
    }
    //屏蔽按钮
    protected void updateStatus1ByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");

        ordersService.updateStatus1ByIds(ids);

        req.getRequestDispatcher("/basedata/orders/findByPage").forward(req, resp);
    }
}
